package dingding;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.taobao.api.ApiException;

/**
 * 返回 钉钉子部门id
 * @author ljl
 *
 */
public class DepartMentSonList {

	public List<String> getDepartMentSon(String deptId) throws Exception {
		ArrayList<String> arrayList=new ArrayList<String>(); //子部门id
		// 获取服务端接口调用凭证access_token
	    String access_token = AccessTokenUtil.getToken();
		DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsubid");
		OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
		req.setDeptId(Long.parseLong(deptId));
		req.setLanguage("zh_CN");
		OapiV2DepartmentListsubResponse rsp = client.execute(req, access_token);
		
		 // 把json字符串转为json对象
        JSONObject jsonObj = JSONObject.parseObject(rsp.getBody());
        String result = jsonObj.getString("result");
 
        JSONObject jsonObj2 = JSONObject.parseObject(result);
        String result2 = jsonObj2.getString("dept_id_list");
        JSONArray arry = JSONObject.parseArray(result2);
        
        for (Object rs : arry) {
			arrayList.add(rs.toString());
		}
       
		return arrayList;
	}
	
}
